package com.system.borrow.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.bean.CustomerDTO;
import com.system.borrow.bean.GloData;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SharedPreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/12/2 13:24
 * 描    述：TODO
 * ================================================
 */
public class InfoFragment extends Fragment implements View.OnClickListener {
    View myView;
    String[] job = {"请选择", "上班族", "个体户", "企业主", "自由职业", "学生"};
    String[] sr = {"请选择", "银行代发", "转账工资", "现金发放"};
    String[] gs = {"请选择", "普通企业", "国企", "世界500强", "上市企业", "其他"};
    String[] gzsj = {"请选择", "未满3月", "3-5月", "6-12月", "1-3年", "4-7年", "7年以上"};
    String[] jn = {"请选择", "是", "否"};
    private Spinner spinner_job, spinner_sr, spinner_gs, spinner_gzsj, spinner_gjj, spinner_sb;
    private EditText et_sr;
    long profession, incomeType, companyType, workTime, sixProvident, sixSocial;
    String income;//月收入
    private Button btn_save;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (myView == null) {
            myView = inflater.inflate(R.layout.fragment_info, container, false);
        } else {
            ViewParent parent = myView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(myView);
            }
        }
        initView(myView);
        addAction();
        return myView;
    }

    private void addAction() {
        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, job);

        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_job.setAdapter(adapter);
        spinner_job.setSelection((int) GloData.getCustomerDTO().getProfession(), true);
        spinner_job.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                profession = position;
                GloData.getCustomerDTO().setProfession(profession);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, sr);

        //设置下拉列表的风格
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_sr.setAdapter(adapter2);
        spinner_sr.setSelection((int) GloData.getCustomerDTO().getIncomeType(), true);
        spinner_sr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                incomeType = position;
                GloData.getCustomerDTO().setIncomeType(incomeType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, gs);

        //设置下拉列表的风格
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_gs.setAdapter(adapter3);
        spinner_gs.setSelection((int) GloData.getCustomerDTO().getCompanyType(), true);
        spinner_gs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                companyType = position;
                GloData.getCustomerDTO().setCompanyType(companyType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, gzsj);

        //设置下拉列表的风格
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_gzsj.setAdapter(adapter4);
        spinner_gzsj.setSelection((int) GloData.getCustomerDTO().getWorkTime(), true);
        spinner_gzsj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                workTime = position;
                GloData.getCustomerDTO().setWorkTime(workTime);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, jn);

        //设置下拉列表的风格
        adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_gjj.setAdapter(adapter5);
        spinner_gjj.setSelection((int) GloData.getCustomerDTO().isSixProvident(), true);
        spinner_gjj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sixProvident = position;
                GloData.getCustomerDTO().setSixProvident(sixProvident);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, jn);

        //设置下拉列表的风格
        adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_sb.setAdapter(adapter6);
        spinner_sb.setSelection((int) GloData.getCustomerDTO().isSixsocial(), true);
        spinner_sb.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                sixSocial = position;
                GloData.getCustomerDTO().setSixsocial(sixSocial);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        et_sr.setText(GloData.getCustomerDTO().getIncome());
    }

    private void initView(View myView) {
        spinner_job = (Spinner) myView.findViewById(R.id.spinner_job);
        spinner_sr = (Spinner) myView.findViewById(R.id.spinner_sr);
        spinner_gs = (Spinner) myView.findViewById(R.id.spinner_gs);
        spinner_gzsj = (Spinner) myView.findViewById(R.id.spinner_gzsj);
        spinner_gjj = (Spinner) myView.findViewById(R.id.spinner_gjj);
        spinner_sb = (Spinner) myView.findViewById(R.id.spinner_sb);
        et_sr = (EditText) myView.findViewById(R.id.et_sr);
        btn_save = (Button) myView.findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save://profession, incomeType, companyType, workTime, sixProvident, sixSocial;
                String income = et_sr.getText().toString().trim();
                GloData.getCustomerDTO().setIncome(income);
                if (GloData.getCustomerDTO().getProfession() == 0) {
                    Toast.makeText(getActivity(), "请选择职业身份", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (profession == 0) {
                        profession = GloData.getCustomerDTO().getProfession();
                    }
                }

                if (GloData.getCustomerDTO().getIncomeType() == 0) {
                    Toast.makeText(getActivity(), "请选择收入形式", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (incomeType == 0) {
                        incomeType = GloData.getCustomerDTO().getIncomeType();
                    }
                }

                if (GloData.getCustomerDTO().getIncome() == null ||GloData.getCustomerDTO().getIncome().isEmpty()) {
                    Toast.makeText(getActivity(), "请填写月收入", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (income == null) {
                        income = GloData.getCustomerDTO().getIncome();
                    }
                }

                if (GloData.getCustomerDTO().getCompanyType() == 0) {
                    Toast.makeText(getActivity(), "请选择公司类型", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (companyType == 0) {
                        companyType = GloData.getCustomerDTO().getCompanyType();
                    }
                }
                if (GloData.getCustomerDTO().getWorkTime() == 0) {
                    Toast.makeText(getActivity(), "请选择当前公司工作时间", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (workTime == 0) {
                        workTime = GloData.getCustomerDTO().getWorkTime();
                    }
                }
                if (GloData.getCustomerDTO().isSixProvident() == 0) {
                    Toast.makeText(getActivity(), "请选择公积金情况", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (sixProvident == 0) {
                        sixProvident = GloData.getCustomerDTO().isSixProvident();
                    }
                }
                if (GloData.getCustomerDTO().isSixsocial() == 0) {
                    Toast.makeText(getActivity(), "请选择社保情况", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (sixSocial == 0) {
                        sixSocial = GloData.getCustomerDTO().isSixsocial();
                    }
                }
                saveData(income);
                break;
        }
    }

    /**
     * @param income
     */

    private void saveData(String income) {
        HttpParams params = new HttpParams();
        params.put("profession", profession + "");
        params.put("incomeType", incomeType + "");
        params.put("income", income);
        params.put("companyType", companyType + "");
        params.put("workTime", workTime + "");
        params.put("sixProvident", sixProvident + "");
        params.put("sixSocial", sixSocial + "");
        OkHttpUtils.post(URLDefind.EDIT)
                .tag(this)
                .params(params)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new DialogCallback<String>(getActivity(), String.class, null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int statusCode = jsonObject.getInt("statusCode");
                            Gson gson = new Gson();
                            if (statusCode == 200) {
                                String data = jsonObject.getString("data");
                                TypeToken<CustomerDTO> token = new TypeToken<CustomerDTO>() {
                                };
                                CustomerDTO customer = gson.fromJson(data, token.getType());
                                GloData.setCustomerDTO(customer);
                                SharedPreferencesUtil.putString(getActivity(), "custormer", "custormer", gson.toJson(customer));
                                Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }
}
